﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;
using Entities;
using Datalayer;

namespace Common
{
    public static class Cl_Convert
    {
        public static DataTable ToDataTable<T>(this IList<T> list)
        {
            PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));
            DataTable table = new DataTable();
            for (int i = 0; i < props.Count; i++)
            {
                PropertyDescriptor prop = props[i];
                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
            }
            object[] values = new object[props.Count];
            foreach (T item in list)
            {
                for (int i = 0; i < values.Length; i++)
                    values[i] = props[i].GetValue(item) ?? DBNull.Value;
                table.Rows.Add(values);
            }
            return table;
        }
        public static string ThuNhap(string tongthu, string tongchi)
        {
            decimal s = 0;
            s = decimal.Parse(tongthu) - decimal.Parse(tongchi);
            return s.ToString();
        }
        public static int getLoaiByTongThuNhap(decimal TTT)
        {
            if (TTT <= 200)
            {
                return 1;
            }
            if(200 < TTT && TTT <= 300)
            {
                return 2;
            }
            if (300 < TTT && TTT <= 400)
            {
                return 3;
            }
            if (400 < TTT && TTT <= 500)
            {
                return 4;
            }
            if (500 < TTT && TTT <= 600)
            {
                return 5;
            }
            if (600 < TTT && TTT <= 700)
            {
                return 6;
            }
            if (700 < TTT && TTT <= 800)
            {
                return 7;
            }
            if (800 < TTT && TTT <= 900)
            {
                return 8;
            }
            if (900 < TTT && TTT <= 1000)
            {
                return 9;
            }
            if (1000 < TTT && TTT <= 1100)
            {
                return 10;
            }
            else
            {
                return 11;
            }
        }
        public static decimal TongThuNhap(string IDHoNgheo)
        {
            decimal tongthu = 0;
            decimal tongchi = 0;
            IQueryMgr iq = QueryMgr.GetDbInstance();
            List<ChiTiet_ThuNhapHoGD> tn = new List<ChiTiet_ThuNhapHoGD>();
            tn = iq.CTTNHGD_GetChiTiet_ThuNhapHoGDByIDHoNgheo(IDHoNgheo);
            for (int i = 0; i < tn.Count; i++)
            {
                tongthu = tongthu + tn[i].Thu;
                tongchi = tongchi + tn[i].Chi;
            }

            return tongthu - tongchi;
        }
        public static decimal TongChiTieu(string IDHoNgheo)
        {
            decimal Tongchi = 0;
            IQueryMgr iq = QueryMgr.GetDbInstance();
            List<ChiTiet_ChiTieuHGD> tn = new List<ChiTiet_ChiTieuHGD>();
            tn = iq.CTCTHGD_GetChiTiet_ChiTieuHGDByIDHoNgheo(IDHoNgheo);
            for (int i = 0; i < tn.Count; i++)
            {
                Tongchi = Tongchi + tn[i].Tien;
            }
            return Tongchi;
        }
    }
}
